﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Dogshow.Win.DataAccess;
using Dogshow.Win.Dialogs;
using Dogshow.Win.Models;
using Ninject;
using System.Linq;
using Dogshow.Model.Extensions;
using Mesto = Dogshow.Model.Mesto;

namespace Dogshow.Win.UserControls
{
    public partial class IzborMesta : DevExpress.XtraEditors.XtraUserControl
    {
        private BindingList<Mesto> m_Mesta = new BindingList<Mesto>();

        public IzborMesta()
        {
            InitializeComponent();
            sifarnikMestaBindingSource.DataSource = m_Mesta;
            lookUpEdit1.ProcessNewValue += (s, e) =>
            {
                var postanskiBroj = e.DisplayValue as string;
                if (postanskiBroj.TrimAndNull() == null) return;
                var mesto = new Models.Mesto { PostanskiBroj = postanskiBroj };
                using (var dlg = new UnosMesta(mesto))
                {
                    if (DialogResult.OK == dlg.ShowDialog(this))
                    {
                        m_Mesta.Add(ModelMapper.Map(mesto));
                        e.Handled = true;
                    }
                }
            };
            lookUpEdit2.ProcessNewValue += (s, e) =>
            {
                var naziv = e.DisplayValue as string;
                if (naziv.TrimAndNull() == null) return;
                var mesto = new Models.Mesto { Naziv = naziv };
                using (var dlg = new UnosMesta(mesto))
                {
                    if (DialogResult.OK == dlg.ShowDialog(this))
                    {
                        m_Mesta.Add(ModelMapper.Map(mesto));
                        e.Handled = true;
                    }
                }
            };
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {
                RefreshData();
            }
        }

        public void RefreshData()
        {
            try
            {
                var mesta = ((IKernel)Bootstrap.Bootstrapper.Container).Get<IDataService>().VratiMesta1().ToList();
                m_Mesta = new BindingList<Mesto>(mesta);
                sifarnikMestaBindingSource.DataSource = mesta;
            }
            catch (Exception exc)
            {
                m_Mesta.Clear();
            }
        }

        

        public void SetupBinding(object dataSource, string dataMember)
        {
            lookUpEdit1.DataBindings.Clear();
            lookUpEdit2.DataBindings.Clear();
            lookUpEdit1.DataBindings.Add("EditValue", dataSource, dataMember, false,
                                         DataSourceUpdateMode.OnPropertyChanged);
            lookUpEdit2.DataBindings.Add("EditValue", dataSource, dataMember, false,
                                         DataSourceUpdateMode.OnPropertyChanged);

        }
    }
}
